$(function(){
	//兼职
	hide();
	//初始化地区
	var arr = area();
		for(var i=0; i<arr.length;i++){
			$("#change").append("<option value="+arr[i].areaCode+">"+arr[i].province+" "+arr[i].city+" "+arr[i].area+"</option>");
	}
	//初始化	
	initFileInput("headUrl", urlcore + "/api/attachment/upload","#oldheadUrl");
	initFileInput("healthCard", urlcore + "/api/attachment/upload","#oldhealthCard");
	initFileInput("licenseUrl", urlcore + "/api/attachment/upload","#oldlicenseUrl");
	initFileInput("identityFront", urlcore + "/api/attachment/upload","#oldidentityFront");

    var userId= getvl("userId");
        $('#thisId').text(userId);
	    $.ajax({
	        url: urlcore + "/api/user/account2Details?userId="+userId,
	        type: "get",
	        dataType: 'json',
	        contentType: "application/json;charset=utf-8",
	        success:function(data){
	            if (data.success == true) {           	
	                $('#realName').val(data.data.user.realName);
	                $('#id').val(data.data.user.secondId);
	                $('#sex').val(data.data.user.sex);
	                $('#phone').val(data.data.user.phone);
	                $('#password').val(data.data.user.password);
	                $('#idCard').val(data.data.user.idCard);
	                $('#serveType').val(data.data.user.serveType);
	                $('#allMoney').val(data.data.allMoney);
	                $('#depositMoney').val(data.data.depositMoney);
	               	$('#dailyTasks').val(data.data.user.dailyTasks);
	                $('#orderTicket').val(data.data.user.orderTicket);
	                $('#dailyDeduction').val(data.data.user.dailyDeduction);
	                $('#restDays').val(data.data.user.restDays);
	                $('#serviceRate').val(parseFloat(data.data.user.serviceRate)*100+"%");
	               	$('#worktimeMon').val(data.data.user.worktimeMon);
	                $('#worktimeEve').val(data.data.user.worktimeEve);
	                $('#changeDays').val(data.data.user.changeDays);
	                $('#isMorning').val(data.data.user.isMorning);
	                $('#fullTime').val(data.data.user.fullTime);
	                var oldheadUrl = data.data.user.headUrl;
	                var oldhealthCard = data.data.user.healthCard;
	                var oldlicenseUrl = data.data.user.licenseUrl;
	                var oldidentityFront = data.data.user.identityFront;
	                
	                if (oldheadUrl != null && oldheadUrl != '') {
	                    var imgarr = oldheadUrl.split("***");
	                    var imgStr = '';
	                    $.each(imgarr,function(index,value){
	                        imgStr += 
	                        	'<div style="position:relative;width:250px;height:250px;float:left;" display="block">'+
	                        	'	<span onclick="removeStrHtml(this)" style="position:absolute;top:10px;right:10px;font-size:20px;color:#f00;" class="glyphicon glyphicon-trash" aria-hidden="true">'+
	                        	'	</span>'+
	                        	'	<img style="padding: 5px 5px 5px 5px;" width="250px" height="250px" src="'+value+'">'+
	                    		'</div>';
	                    });
	                }
	                $('#oldheadUrl').html(imgStr);

	                if (oldlicenseUrl != null && oldlicenseUrl != '') {
	                    var imgarr = oldlicenseUrl.split("***");
	                    var imgStr2 = '';
	                    $.each(imgarr,function(index,value){
	                        imgStr2 += 
	                        	'<div style="position:relative;width:250px;height:250px;float:left;" display="block">'+
	                        	'	<span onclick="removeStrHtml(this)" style="position:absolute;top:10px;right:10px;font-size:20px;color:#f00;" class="glyphicon glyphicon-trash" aria-hidden="true">'+
	                        	'	</span>'+
	                        	'	<img style="padding: 5px 5px 5px 5px;" width="250px" height="250px" src="'+value+'">'+
	                    		'</div>';
	                    });
	                }
	                $('#oldlicenseUrl').html(imgStr2);

	                if (oldhealthCard != null && oldhealthCard != '') {
	                    var imgarr = oldhealthCard.split("***");
	                    var imgStr3 = '';
	                    $.each(imgarr,function(index,value){
	                        imgStr3 += 
	                        	'<div style="position:relative;width:250px;height:250px;float:left;" display="block">'+
	                        	'	<span onclick="removeStrHtml(this)" style="position:absolute;top:10px;right:10px;font-size:20px;color:#f00;" class="glyphicon glyphicon-trash" aria-hidden="true">'+
	                        	'	</span>'+
	                        	'	<img style="padding: 5px 5px 5px 5px;" width="250px" height="250px" src="'+value+'">'+
	                    		'</div>';
	                    });
	                }
	                $('#oldhealthCard').html(imgStr3);	
	                
	                if (oldidentityFront != null && oldidentityFront != '') {
	                    var imgarr = oldidentityFront.split("***");
	                    var imgStr4 = '';
	                    $.each(imgarr,function(index,value){
	                        imgStr4 += 
	                        	'<div style="position:relative;width:250px;height:250px;float:left;" display="block">'+
	                        	'	<span onclick="removeStrHtml(this)" style="position:absolute;top:10px;right:10px;font-size:20px;color:#f00;" class="glyphicon glyphicon-trash" aria-hidden="true">'+
	                        	'	</span>'+
	                        	'	<img style="padding: 5px 5px 5px 5px;" width="250px" height="250px" src="'+value+'">'+
	                    		'</div>';
	                    });
	                }
	                $('#oldidentityFront').html(imgStr4);	
	            } else if (data.code == 'OVERTIME'){
	                var thisUrl = window.location.href;
	                if (thisUrl.indexOf('login.html') <= -1) {
	                    top.window.location.href="login.html";
	                }
	            } else {
	                alert(data.msg);
	            }
	        },
	        error:function() {
	            /* Act on the event */
	            alert("error");
	        }
	    });
	    
	    //地区
	   	$.ajax({
            url: urlcore + "/api/address/addressByUserId?userId="+userId,
            type: "get",
            dataType: 'json',
            contentType: "application/json;charset=utf-8",
            success:function(data){
                if (data.success == true) {
                	var da = data.data;  
	            	$("#change option[value="+da.areaCode+"]").prop("selected", 'selected');                            
                } else if (data.code == 'OVERTIME'){
	                    var thisUrl = window.location.href;
	                    if (thisUrl.indexOf('login.html') <= -1) {
	                        top.window.location.href="login.html";
	                    }
	                } else {
	                    alert(data.msg);
	                }
            },
            error:function() {
                /* Act on the event */
                alert("error");
            }
        });
});


//添加
function release(){
	var userId=$('#thisId').text()
	var realName = $('#realName').val();
	var id = $('#id').val();
	var sex = $('#sex').val();
	var phone = $('#phone').val();
	var password = $('#password').val();
	var idCard = $('#idCard').val();
	var allMoney = $('#allMoney').val();
	var depositMoney = $('#depositMoney').val();
	var dailyTasks = $('#dailyTasks').val();
	var orderTicket = $('#orderTicket').val();
	var dailyDeduction = $('#dailyDeduction').val();
	var restDays = $('#restDays').val();
	var worktimeMon = $('#worktimeMon').val();
	var worktimeEve = $('#worktimeEve').val();
	var changeDays = $('#changeDays').val();
	var isMorning = $('#isMorning').val();
	var fullTime = $('#fullTime').val();
	
	var serviceRate=$("#serviceRate").val().replace(/[~'!<>@#$%^&*()-+_=:]/g, "");
	var realserviceRate=parseFloat(serviceRate)*0.01;
	
	//地区
	var content = $("#change").find("option:selected").text();
	var arr = content.split(' ');
	var provinceName = arr[0];
	var cityName = arr[1];
	var areaName = arr[2];
	var areaCode = $("#change").val();

    //图片地址
    var headUrl = "";
    $("#oldheadUrl img").each(function(index,item){
		headUrl = $(this).attr("src")+"***";
	});
    headUrl = headUrl.substring(0, headUrl.length-3);
    
    var licenseUrl = "";
    $("#oldlicenseUrl img").each(function(index,item){
		licenseUrl = $(this).attr("src")+"***";
	});
    licenseUrl = licenseUrl.substring(0, licenseUrl.length-3);
    
    var healthCard = "";
    $("#oldhealthCard img").each(function(index,item){
		healthCard = $(this).attr("src")+"***";
	});
    healthCard = healthCard.substring(0, healthCard.length-3);
    
    var identityFront = "";
    $("#oldidentityFront img").each(function(index,item){
		identityFront = $(this).attr("src")+"***";
	});
    identityFront = identityFront.substring(0, identityFront.length-3);

    $.ajax({
        url:  urlcore + "/api/user/updateUser?userId="+userId,
        type: "post",
        data:JSON.stringify({
	        "headUrl": headUrl,
	        "licenseUrl":licenseUrl,
	        "healthCard": healthCard,
	        "identityFront": identityFront,
	        "realName":realName,
			"sex":sex,
	        "idCard":idCard,
	        "secondId": id,
	        "phone": phone,
	        "password": password,
	        "idCard":idCard,
	        "dailyTasks": dailyTasks,
	        "orderTicket": orderTicket,
	        "dailyDeduction": dailyDeduction,
	        "serviceRate": realserviceRate,
	        "restDays": restDays,
	       	"worktimeMon": worktimeMon,
	        "worktimeEve": worktimeEve,
	        "isMorning": isMorning,
	        "fullTime": fullTime,
	        "changeDays": changeDays
		}),
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        success:function(data){
            if (data.success == true) {
                    alert("修改成功");
                    //跳转列表页
                    window.location.href = "rider_update.html?userId="+userId;               
            } else if (data.code == 'OVERTIME') {
                var thisUrl = window.location.href;
                if (thisUrl.indexOf('login.html') <= -1) {
                    top.window.location.href = "login.html";
                }
            } else {
                alert(data.msg);
            }

        },
        error: function() {
            alert("error");
        }

    });
    
        //添加地区
    $.ajax({
        url:  urlcore + "/api/address/updateUserAddress?userId="+userId,
        type: "post",
       	data:JSON.stringify({
	        "province":provinceName,
	        "city": cityName,
	        "county": areaName,
	        "areaCode": areaCode
		}),
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        success:function(data){
        	if (data.success == true) {
            } else if (data.code == 'OVERTIME') {
                var thisUrl = window.location.href;
                if (thisUrl.indexOf('login.html') <= -1) {
                    top.window.location.href = "login.html";
                }
            } else {
                alert(data.msg);
            }

        },
        error: function() {
            alert("error");
        }

    });
}

//获取所属区域
function area(){
	var da='';
	$.ajax({
            url: urlcore + "/api/openArea/allArea",
            type: "get",
            dataType: 'json',
            contentType: "application/json;charset=utf-8",
            async: false,
            success:function(data){
                if (data.success == true) {
                    da = data.data;                   
                } else if (data.code == 'OVERTIME'){
                    var thisUrl = window.location.href;
                    if (thisUrl.indexOf('login.html') <= -1) {
                        top.window.location.href="login.html";
                    }
                } else {
                    alert(data.msg);
                }
            },
            error:function() {
                /* Act on the event */
                alert("error");
            }
        });
        return da;
}

//兼职
function hide(){
       $("#fullTime").change(function () {
       		if($("#fullTime").val()==1){
       			$("#hide").css('display','block');
       			$("#hideother").css('display','block');
       		}
       		if($("#fullTime").val()==2){
       			$("#hide").css('display','none');      			
       			$("#hideother").css('display','none');
       			//清空值
       			$("#worktimeMon").val('');
       			$("#worktimeEve").val('');
       			$("#changeDays").val('');
       			$("#dailyTasks").val('');
       			$("#orderTicket").val('');
       			$("#restDays").val('');
       			$("#dailyDeduction").val('');
       			$("#isMorning").val('');      			
       		}
   	   });
}
